package cc.rengu.igas.channel.cups.core.service.trans;


import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BasePayService;
import cc.rengu.igas.channel.cups.facade.request.TransferTrapRevsalRequest;
import cc.rengu.igas.channel.cups.facade.response.TransferTrapRevsalResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;

/**
 * 非指定账户圈存冲正
 *
 * @author who
 */
public class CupsTransferTrapRevsalService extends BasePayService<TransferTrapRevsalRequest, TransferTrapRevsalResponse> {
    @Override
    protected ChnlBizResponse callBizService(CupsFieldBean cupsFieldBean,
                                             TransferTrapRevsalRequest transferTrapRevsalRequest,
                                             TransferTrapRevsalResponse transferTrapRevsalResponse) throws Exception {

        ConvertUtil.convertOutput(cupsFieldBean);
        CupsField cupsField = cupsFieldBean.getCupsField();
        BeanUtil.beanCopy(transferTrapRevsalRequest, cupsField);

        String tmpAmt = AmountUtil.changeY2F(transferTrapRevsalRequest.getOrderAmt());
        cupsField.setOrderAmt(String.format("%012d", Integer.valueOf(tmpAmt)));


        //  终端读取能力
        //  IC卡条件代码
        //  小额免密标志
        //  终端类型
        //  特殊计费类型
        cupsFieldBean.createF60()
                .setF60_1("4021")
                .setF60_2_2(transferTrapRevsalRequest.getTermRead())
                .setF60_2_3(transferTrapRevsalRequest.getIcConCode())
                .setF60_2_5(transferTrapRevsalRequest.getTermType());
        cupsFieldBean.build();

        //  调用 CUPS   TODO 此交易配置暂时有误 ,关联了行内交易码, 后续解耦
        CupsFieldRspBean cupsFieldRspBean = callCups(cupsFieldBean, transferTrapRevsalRequest.getHeader().getTxnNum());

        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, transferTrapRevsalResponse);
        transferTrapRevsalResponse.setRespCode(cupsFieldRsp.getRespCode());
        if (CupsParamConstant.SUCCESS_CODE.equals(transferTrapRevsalResponse.getRespCode())) {
            transferTrapRevsalResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            transferTrapRevsalResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        }
        return new ChnlBizResponse(transferTrapRevsalResponse);
    }
}
